package com.hx.er.service.customer.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.hx.er.bean.base.UserBean;
import com.hx.er.common.entity.LoginInfoEntity;
import com.hx.er.dao.IUserDao;
import com.hx.er.service.customer.ICustomerUserService;

public class CustomerUserServiceImpl implements ICustomerUserService {
	@Autowired
	private IUserDao userDao;

	@Override
	public int updateUserInfo(UserBean userBean) {
		return this.userDao.updateUserInfo(userBean);
	}

	@Override
	public int updateUserPassword(Map<String, Object> params) {
		return this.userDao.updateUserPassword(params);
	}

	@Override
	public UserBean getUserAliveById(long userId) {
		return this.userDao.getUserAliveById(userId);
	}

	@Override
	public void insertLoginInfoWithoutId(LoginInfoEntity loginInfoEntity) {
		this.userDao.insertLoginInfoWithoutId(loginInfoEntity);
	}

	@Override
	public List<LoginInfoEntity> getLoginInfoList(LoginInfoEntity loginInfoEntity) {
		return this.userDao.getLoginInfoList(loginInfoEntity);
	}

	@Override
	@Transactional
	public Map<String, Boolean> insertCustomerWithoutId(UserBean userBean) {
		// 用户名、邮箱、手机号码冲突
		int countUserName = this.userDao.checkUserName(userBean);
		int countMobile = this.userDao.checkMobile(userBean);
		int countMail = this.userDao.checkMail(userBean);

		// 初始化返回结果
		Map<String, Boolean> msgMap = new HashMap<String, Boolean>();
		if (countUserName > 0) {
			msgMap.put("msg.userName", true);
		}
		if (countMobile > 0) {
			msgMap.put("msg.mobile", true);
		}
		if (countMail > 0) {
			msgMap.put("msg.mail", true);
		}

		// 如果map中是空，则插入后返回，如果map中有错误信息，则直接返回
		if (msgMap.isEmpty()) {
			// 插入数据
			this.userDao.insertCustomerWithoutId(userBean);
		}

		return msgMap;
	}

	@Override
	public UserBean getUserAlive(UserBean userBean) {
		return this.userDao.getUserAlive(userBean);
	}
}
